package cn.com.customer.manager.ui.login

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import cn.com.customer.manager.R
import cn.com.customer.manager.base.BaseActivity
import cn.com.customer.manager.ui.main.MainActivity
import cn.com.customer.manager.ui.regist.FindPasswordActivity
import cn.com.customer.manager.ui.regist.RegistActivity
import com.xuexiang.xui.utils.SpanUtils
import com.xuexiang.xutil.data.SPUtils
import com.xuexiang.xutil.tip.ToastUtils
import kotlinx.android.synthetic.main.activity_account_login.*


class AccountLoginActivity : BaseActivity(), AccountLoginContract.View {

    private lateinit var mPresenter: AccountLoginContract.Presenter
    /**
     * 获取layout资源
     */
    override fun getLayoutId(): Int = R.layout.activity_account_login

    /**
     * 初始化控件
     */
    @SuppressLint("ResourceAsColor")
    override fun initView(savedInstanceState: Bundle?) {
        button_login.setOnClickListener(this)
        forget_pass.setOnClickListener(this)
        register.setOnClickListener(this)
        AccountLoginPresenter(this)
        initdata()
        val builder: SpannableStringBuilder = SpanUtils().append("登录注册代表您已阅读并同意").setForegroundColor(R.color.base_text)
                .append("用户协议").setForegroundColor(R.color.colorPrimary).setClickSpan(object : ClickableSpan() {
                    override fun onClick(p0: View) {
                        ToastUtils.toast("点击了用户协议")
                    }

                }).append("和").append("隐私政策").setForegroundColor(R.color.colorPrimary).setClickSpan(object : ClickableSpan() {
                    override fun onClick(p0: View) {
                        ToastUtils.toast("点击了隐私政策")
                    }
                }).create()
        login_agreement.text = builder
        login_agreement.movementMethod = LinkMovementMethod.getInstance()


    }

    private fun initdata() {
     if (SPUtils.getBoolean(SPUtils.getDefaultSharedPreferences(),"remember",false)){
         login_name.setText(SPUtils.getString(SPUtils.getDefaultSharedPreferences(),"userName",name))
         login_pass.setText(SPUtils.getString(SPUtils.getDefaultSharedPreferences(),"passWord",passWord))
         remember.isChecked=true
     }
    }

    /**
     * 设置Presenter
     */
    override fun setPresenter(presenter: AccountLoginContract.Presenter) {
        mPresenter = presenter
    }

    var name=""
    var passWord=""
    /**
     * 登录前，获取手机号码和密码，并判断
     */
    override fun onBeforeLogin() {
         name=login_name.text.toString()
         passWord=login_pass.text.toString()
        if (name.isNullOrEmpty()){
            ToastUtils.toast(getString(R.string.login_tip1))
            return
        }else if (passWord.length<6||passWord.length>12){
            ToastUtils.toast(R.string.input_pwd_format)
            return
        }
       mPresenter.login("DLSCS","123456")

    }

    /**
     * 登录成功
     */
    override fun loginSuccess() {
        if (remember.isChecked){
            SPUtils.put(SPUtils.getDefaultSharedPreferences(),"userName",name)
            SPUtils.put(SPUtils.getDefaultSharedPreferences(),"passWord",passWord)
            SPUtils.put(SPUtils.getDefaultSharedPreferences(),"remember",true)
        }else{
            SPUtils.put(SPUtils.getDefaultSharedPreferences(),"remember",false)
            SPUtils.put(SPUtils.getDefaultSharedPreferences(),"userName","")
            SPUtils.put(SPUtils.getDefaultSharedPreferences(),"passWord","")
        }
        startActivity(MainActivity::class.java)
        finish()

    }

    /**
     * 登录失败
     */
    override fun loginFailure() {
    }


    /**
     * 点击事件
     */
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.button_login -> {
                onBeforeLogin()
            }
            R.id.forget_pass -> {
                startActivity(Intent(this,FindPasswordActivity::class.java))
            }
            R.id.register -> {
                startActivity(Intent(this,RegistActivity::class.java))
            }
        }
    }
}
